﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Samurai.Validation.Specs.StaticMocks;
using Samurai.Wakizashi.Validation.Reflection;

namespace Samurai.Validation.Specs
{
    [TestClass]
    public class ReflectionRegistry_Specification
    {
        [TestMethod]
        public void Caches_rules_applied_to_a_given_types_property()
        {
            var map = ReflectionRegistry.Instance.GetAllRulesAppliedToType(typeof (Customer));
            Assert.IsTrue(map["Age"].Count > 0);
        }

        [TestMethod]
        public void Caches_all_rules_applied_to_a_validated_types_properties()
        {
            Assert.IsTrue(ReflectionRegistry.Instance.GetAllRulesAppliedToType(typeof(Customer)).Count > 1);
        }

        [TestMethod]
        public void Associates_property_with_rule_in_provided_map()
        {
            //Get list of rules again. Property under validation should be null this time because a newly
            //  cloned copy of the rule is provided
            var map = ReflectionRegistry.Instance.GetAllRulesAppliedToType(typeof(Customer));
            Assert.IsNotNull(map["Age"][0].PropertyUnderValidation.PropertyInformation);
        }

        [TestMethod]
        public void Provides_cloned_copy_of_rules()
        {
            var map = ReflectionRegistry.Instance.GetAllRulesAppliedToType(typeof(Customer));
            var c = new Customer();
            map["Age"][0].PropertyUnderValidation.Owner = c;

            //Get list of rules again. Property under validation should be null this time because a newly
            //  cloned copy of the rule is provided
            map = ReflectionRegistry.Instance.GetAllRulesAppliedToType(typeof(Customer));
            Assert.IsNull(map["Age"][0].PropertyUnderValidation.Owner);
        }
    }
}
